package com.yusvn.scm.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Inject;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.plugin.activerecord.Page;
import com.yusvn.scm.admin.service.AdminRoleService;
import com.yusvn.scm.common.BaseController;
import com.yusvn.scm.common.kit.RetKit;
import com.yusvn.scm.common.model.SysRole;

/**
 * 角色管理
 * @author liugz
 *
 */
@EnableCORS
public class AdminRoleController extends BaseController {

	@Inject
	AdminRoleService srv;
	
	public void index() {
		renderJson();
	}
	
	public void page() {
		Page<SysRole> list = srv.page(getKv());
		
		renderJson(RetKit.ok().setCount(list.getTotalRow()).setData( JSONObject.toJSON(list.getList()) ) );
	}

	public void get() {
		renderJson(RetKit.ok().setData(srv.get(getPara("roleId") ) ));
	}

	public void list() {
		renderJson(RetKit.ok().setData( JSONObject.toJSON(srv.list()) ));
	}
	
	public void save() {
		renderJson(srv.save(getRawData()));
	}

	public void update() {
        renderJson(srv.update(getRawData()));
	}

	public void remove() {
		renderJson(srv.remove(getPara("id")));
	}

	public void removeBatch() {
		renderJson(srv.removeBatch(getRawData() ) );
	}
	
	public void assignMenu() {
		String roleId = getPara("roleId");
		
		renderJson(srv.assignMenu(roleId));
	}
	
	public void saveMenu() {
		JSONObject jsonObj = JSONObject.parseObject(getRawData());
		int roleId = jsonObj.getIntValue("roleId");
		JSONArray menuIds = jsonObj.getJSONArray("menuIds");
		
		renderJson(srv.saveMenu(roleId, menuIds));
	}
	
}
